﻿package fj.media
{
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.media.Video;
	import flash.media.Camera;
	
	public class FJWebcam extends MovieClip
	{
		private var _camera:Camera;
		private var _video:Video;
		private var _capture:MovieClip;
		private var _bitmapData:BitmapData;
		private var _bitmap:Bitmap;
		
		public function FJWebcam():void
		{
			_video = video.getChildByName("video") as Video;
			_capture = getChildByName("capture") as MovieClip;
			
			_camera = Camera.getCamera();
			_camera.setMode( video.width, video.height, 25 );
		}
		public function start():void
		{
			_video.attachCamera( _camera );
		}
		public function resolution( w:Number, h:Number, fps:Number ):void
		{
			_camera.setMode( w, h, fps);
		}
		public function shot():void
		{
			_bitmapData = new BitmapData( video.width, video.height );
			_bitmapData.draw( video );
			_bitmap = new Bitmap( _bitmapData );
			capture.addChild( _bitmap );
		}
		public function drop():void
		{
			if(_bitmap){
				_capture.removeChild( _bitmap );
				_bitmap = null;
			}
		}
		public function get bitmapData():BitmapData
		{
			return _bitmapData;
		}
		public function get bitmap():Bitmap
		{
			return _bitmap;
		}
	}
}